<?php
namespace app\index\controller;

use think\Controller;
use think\Db;
use think\facade\Config;

class DbTest extends Controller
{
    /**
     * 测试数据库配置
     */
    public function testConfig()
    {
        try {
            // 获取数据库配置
            $dbConfig = Config::get('database.');
            
            return json([
                'code' => 1,
                'msg' => '配置获取成功',
                'data' => [
                    'default' => $dbConfig['default'] ?? 'not set',
                    'mysql_config' => $dbConfig['connections']['mysql'] ?? 'not set',
                    'all_config' => $dbConfig
                ]
            ]);
        } catch (\Exception $e) {
            return json([
                'code' => 0,
                'msg' => '配置获取失败: ' . $e->getMessage()
            ]);
        }
    }
    
    /**
     * 测试数据库连接
     */
    public function testConnection()
    {
        
        
        try {
            // 直接使用配置测试连接
            $config = [
                'type'     => 'mysql',
                'hostname' => '8.155.50.137',
                'database' => 'testdemo',
                'username' => 'testdemo',
                'password' => 'LsCPWTCxDakxRZ24',
                'hostport' => '3306',
                'charset'  => 'utf8',
            ];
            
            // 创建临时连接
            $connection = Db::connect($config);
            $result = $connection->query("SELECT 1 as test");
            
            return json([
                'code' => 1,
                'msg' => '直接连接成功',
                'data' => $result
            ]);
        } catch (\Exception $e) {
            return json([
                'code' => 0,
                'msg' => '直接连接失败: ' . $e->getMessage()
            ]);
        }
    }
    
    /**
     * 测试第二个数据库连接
     */
    public function testSecondDb()
    {
        try {
            // 使用第二个数据库连接
            $result = Db::connect('db_config2')->query("SELECT 1 as test");

            return json([
                'code' => 1,
                'msg' => '第二个数据库连接成功',
                'data' => $result
            ]);
        } catch (\Exception $e) {
            return json([
                'code' => 0,
                'msg' => '第二个数据库连接失败: ' . $e->getMessage()
            ]);
        }
    }

    /**
     * 测试两个数据库连接
     */
    public function testBothDb()
    {
        $results = [];

        // 测试默认数据库
        try {
            $result1 = Db::query("SELECT 'default_db' as db_name, NOW() as current_time");
            $results['default_db'] = [
                'status' => 'success',
                'data' => $result1
            ];
        } catch (\Exception $e) {
            $results['default_db'] = [
                'status' => 'error',
                'message' => $e->getMessage()
            ];
        }

        // 测试第二个数据库
        try {
            $result2 = Db::connect('db_config2')->query("SELECT 'second_db' as db_name, NOW() as current_time");
            $results['second_db'] = [
                'status' => 'success',
                'data' => $result2
            ];
        } catch (\Exception $e) {
            $results['second_db'] = [
                'status' => 'error',
                'message' => $e->getMessage()
            ];
        }

        return json([
            'code' => 1,
            'msg' => '双数据库测试完成',
            'data' => $results
        ]);
    }
    
    /**
     * 测试默认连接
     */
    public function testDefaultConnection()
    {

        try {
            // 使用默认连接
            $result = Db::query("SELECT 1 as test");
            
            return json([
                'code' => 1,
                'msg' => '默认连接成功',
                'data' => $result
            ]);
        } catch (\Exception $e) {
            return json([
                'code' => 0,
                'msg' => '默认连接失败: ' . $e->getMessage()
            ]);
        }
    }
}
